' =================================================================
'  This program test if some COM device sends continuosly N-Values
'  Then decodes N-Values and send them to slots
' =================================================================


' ----------------------------------- Initializations
Numeric FirstSlot = 110
Numeric NumValues = 4
Numeric BaudRate = 115200

' ----------------------------------- Buttons
Button 1 Label FreqSmooth
Button 1 Image0S Button1.png

Button 2 Label FreqCalib
Button 2 Image0S Button1.png

Button 3 Label FreqChangeRate
Button 3 Image0S Button1.png

Button 4 Label FreqOffset
Button 4 Image0S Button1.png

' ----------------------------------- Load Apps
'Load ..\APPS\Theremino_SlotViewer.exe
Load ..\APPS\Theremino_SignalScope.exe

' ----------------------------------- Start
OpenComPort
Wait Seconds 0.5
Goto Loop
Stop

Label EventStop
	COM Close
End


' -----------------------------------------------
'  OPEN COM
' -----------------------------------------------
Label OpenComPort
	Numeric Bauds = BaudRate
	String  ComPort
	Numeric ComIndex
	Numeric ComRetry1
	Numeric ComRetry2
	String  ComReceived
	For ComRetry1 = 1 To 10
		For ComIndex = 0 To GetSeparatedStringCount(COM_PortNames) - 1
			ComPort = GetSeparatedString(COM_PortNames, ComIndex)
			COM Open ComPort Bauds
			If COM_Status = "OPENED"
				For ComRetry2 = 1 To 3
					COM DiscardOutBuffer
					COM DiscardInBuffer
					Wait Seconds 0.01
					ComReceived = COM_ReceiveUntil(LF)
					If GetSeparatedStringCount(ComReceived) = NumValues
						ComIndex = -1
						Beep "440-1,0-80,880-1"
						Return
					EndIf
				Next
			EndIf
		Next
	Next
	If ComIndex >= 0
		Print "COM PORT NOT FOUND"
		'TTS SelectVoice "ENGF"
		'TTS Speak "Com port not found."
		'TTS Speak "Please test connections and retry."
		Wait Not TTSspeaking
		End
	EndIf
Return

' =====================================================
'  LOOP
' =====================================================
Numeric Decoded
String ComStr
String DecodeStr
String DecodeStrOld
Numeric Counter

Label Loop
	' ------------------------------------------- Receive
	For Counter = 1 To Infinity
		ComStr = COM_ReceiveUntil(CRLF)
		If ComStr <> ""
			DecodeStr = ComStr
		Else
			Exit
		EndIf
		If Counter > 5
			COM Open ComPort Bauds ' Clear COM buffers
		EndIf
	Next
	If DecodeStr <> DecodeStrOld
		For Counter = 0 To NumValues - 1
			Decoded = GetSeparatedString(DecodeStr, Counter)
			Slot FirstSlot + Counter = Decoded
			Button Counter+1 Text Decoded
		Next
		' ------------------------------------------- Print
		Print DecodeStr
		' -------------------------------------------
		DecodeStrOld = DecodeStr
	EndIf
	' ------------------------------------------- Loop time
	Wait Seconds 0.001
	' ------------------------------------------- Debugs
    'PrintLoopTime
Goto Loop

' =====================================================
'  DEBUG
' =====================================================
Label PrintLoopTime
	Numeric newlooptime = ElapsedTime
	Numeric looptime = newlooptime - oldlooptime
	Numeric oldlooptime = newlooptime
	Print Format(looptime * 1000, "0.0")
Return

